package com.one.efaimaly.order.ui.fragment;

import android.view.View;

import com.one.common.callback.OnBinderItemClickListener;
import com.one.common.common.order.AddMaterialItem;
import com.one.common.common.order.binder.AddMaterialBinder;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.event.Subscribe;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.Logger;
import com.one.common.utils.StringUtils;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListFragment;
import com.one.efaimaly.order.model.event.AddMaterialEvent;
import com.one.efaimaly.order.model.extra.MaterialFragmentExtra;
import com.one.efaimaly.order.model.item.AddItem;
import com.one.efaimaly.order.presenter.MaterialPresenter;
import com.one.efaimaly.order.ui.binder.AddBtnBinder;

import java.util.ArrayList;
import java.util.List;

/**
 * creator: zhulunjun
 * time:    2018/8/15
 * describe:
 */
public class AddMFragment extends BaseListFragment<MaterialPresenter> implements IListView,
        OnBinderItemClickListener<AddMaterialItem> {

    private int curPosition = 0;
    private boolean isFree;
    private int type;
    private MaterialFragmentExtra extra;

    @Override
    protected void initPresenter() {
        mPresenter = new MaterialPresenter(this, getActivity());
    }

    @Override
    public void registerDate() {
        register(AddMaterialItem.class, new AddMaterialBinder(this));
        register(AddItem.class, new AddBtnBinder(new OnBinderItemClickListener<AddItem>() {
            @Override
            public void onItemClick(View v, int position, AddItem o) {
                addData(getData().size() - 1, new AddMaterialItem(isFree));
            }
        }));
    }

    @Override
    public void loadData() {
        addData(new AddMaterialItem(isFree));
        addData(new AddItem());
    }

    @Override
    public void initView() {
        super.initView();
        hindLoadMore();
        hindRefresh();
    }

    @Override
    public void onItemClick(View v, int position, AddMaterialItem o) {
        curPosition = position;
        RouterManager.getInstance().goForResult(getActivity(), RouterPath.SELECT_MATERIAL, new DefaultExtra(type));
    }

    @Override
    public void initData() {
        super.initData();
        extra = (MaterialFragmentExtra) getArguments()
                .getSerializable(MaterialFragmentExtra.getExtraName());
        if (extra != null) {
            type = extra.getType();
            isFree = type == AddMaterialEvent.FREE;
            for (AddMaterialItem item : extra.getData()) {
                addData(item);
            }
            if (!extra.isUpdate()) {
                loadData();
            }
        } else {
            loadData();
        }
    }

    @Subscribe
    public void onSelectM(AddMaterialEvent event) {
        if (event.getType() == type) {
            AddMaterialItem addMaterialItem = (AddMaterialItem) getData().get(curPosition);
            if (StringUtils.isNotBlank(addMaterialItem.getName())) {
                if (!addMaterialItem.getName().equals(event.getName())) {
                    addMaterialItem.setName(event.getName());
                    // 清空详情带过来的id
                    addMaterialItem.setOrder_material_id("");
                }
            } else {
                addMaterialItem.setName(event.getName());
            }
            getAdapter().notifyItemChanged(curPosition);
        }
    }

    public List<AddMaterialItem> getMaterialList() {
        if (mPresenter == null) {
            Logger.d("空的");
            return new ArrayList<>();
        }
        return mPresenter.getAddMaterials();
    }
}
